#include <stdlib.h>
#include <stdio.h>
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include <SDL/SDL_ttf.h>
#define sprite_vitesse 70
#define NUM_TRASH 2
#define MAX_SPEED 4



static SDL_Surface * ecran;
static SDL_Surface * trashs;


int main (int args, char * argv[] )
{
int i,face;

int continuer=1, tir = 0,cpt=0;


  SDL_Init( SDL_INIT_EVERYTHING );
  SDL_Surface  * bg=NULL,* perso[4],*saut[8],*frappe[7],*jauge[15],*bullet1[3],*bullet2[3];

  ecran=SDL_SetVideoMode( 900,700 , 32 ,SDL_HWSURFACE );
  bg=IMG_Load("bgmehdi.jpg");
  SDL_Rect position;
  position.x=0;
  position.y=0;
  position.h=bg->h;
  position.w=bg->w=877;





perso[0]=IMG_Load("perso1.png");
perso[1]=IMG_Load("perso2.png");
perso[2]=IMG_Load("reverse.png");
perso[3]=IMG_Load("reverse2.png");



 SDL_Rect persoPosition;
 persoPosition.x=300;
 persoPosition.y=600;


trashs=IMG_Load("Trash1.png");


SDL_Rect trashposition;
trashposition.x=690;
trashposition.y=600;


saut[0]=IMG_Load("saut1.png");
saut[1]=IMG_Load("saut2.png");
saut[2]=IMG_Load("saut3.png");
saut[3]=IMG_Load("saut4.png");
saut[4]=IMG_Load("s1.png");
saut[5]=IMG_Load("s2.png");
saut[6]=IMG_Load("s3.png");
saut[7]=IMG_Load("s4.png");


frappe[0]=IMG_Load("waterman 1.png");
frappe[1]=IMG_Load("waterman 2.png");
frappe[2]=IMG_Load("waterman 3.png");
frappe[3]=IMG_Load("waterman 4.png");
frappe[4]=IMG_Load("r1.png");
frappe[5]=IMG_Load("r2.png");
frappe[6]=IMG_Load("r3.png");
frappe[7]=IMG_Load("r4.png");

jauge[0]=IMG_Load("1.jpg");
jauge[1]=IMG_Load("2.jpg");
jauge[2]=IMG_Load("3.jpg");
jauge[3]=IMG_Load("4.jpg");
jauge[4]=IMG_Load("5.jpg");
jauge[5]=IMG_Load("6.jpg");
jauge[6]=IMG_Load("7.jpg");
jauge[7]=IMG_Load("8.jpg");
jauge[8]=IMG_Load("9.jpg");
jauge[9]=IMG_Load("10.jpg");
jauge[10]=IMG_Load("11.jpg");
jauge[11]=IMG_Load("12.jpg");
jauge[12]=IMG_Load("13.jpg");
jauge[13]=IMG_Load("14.jpg");
jauge[14]=IMG_Load("15.jpg");
SDL_Rect jaugeposition;
jaugeposition.x=700;
jaugeposition.y=10;



bullet2[0]=IMG_Load("b1.png");
bullet2[1]=IMG_Load("b2.png");
bullet2[2]=IMG_Load("b3.png");
bullet2[3]=IMG_Load("b4.png");
bullet1[0]=IMG_Load("b5.png");
bullet1[1]=IMG_Load("b6.png");
bullet1[2]=IMG_Load("b7.png");
bullet1[3]=IMG_Load("b8.png");
SDL_Rect bulletposition1;
bulletposition1.x=300;
bulletposition1.y=600;
bulletposition1.w=bullet1[0]->w;
bulletposition1.h=bullet1[0]->h;


SDL_Rect bulletposition2;
bulletposition2.x=300;
bulletposition2.y=600;
bulletposition2.w=bullet2[0]->w;
bulletposition2.h=bullet2[0]->h;

SDL_BlitSurface(bullet1[0],NULL,ecran,&bulletposition1);
SDL_BlitSurface(bullet2[0],NULL,ecran,&bulletposition2);
SDL_BlitSurface(jauge[0],NULL,ecran,&jaugeposition);

SDL_BlitSurface(bg,NULL,ecran,&position);



SDL_BlitSurface(trashs,NULL,ecran,&trashposition);
SDL_BlitSurface(frappe[0],NULL,ecran,&persoPosition);










while(continuer)
{
position.x++;
if(trashposition.x>ecran->w-600)
trashposition.x++;
if(trashposition.x>ecran->w-70)
trashposition.x--;


SDL_BlitSurface(bg,NULL,ecran,&position);
  SDL_BlitSurface(trashs,NULL,ecran,&trashposition);


 SDL_Event event;
     SDL_PollEvent(&event);

if(event.type==SDL_QUIT)
{
break;
}







if(event.type==SDL_KEYDOWN)
{


if(event.key.keysym.sym==SDLK_ESCAPE)
{ continuer=0;
break ;


}

if(event.key.keysym.sym==SDLK_RIGHT)

{

persoPosition.x+=20;
face=1;
if(cpt>=1)
cpt=-1;
cpt++;
}

if(event.key.keysym.sym==SDLK_LEFT)

{
persoPosition.x-=20;

face=2;
if(cpt>=3)
cpt=1;
cpt++;
}


if(event.key.keysym.sym==SDLK_a)
{

tir = 1;

if(face==2)
{
bulletposition2.x=persoPosition.x-10;
for(i=4;i<9;i++)
{


  SDL_BlitSurface(frappe[i],NULL,ecran,&persoPosition);

  SDL_Delay(sprite_vitesse);


}






}
if(face==1)
{
bulletposition1.x=persoPosition.x+10;
for(i=0;i<4;i++)
{



  SDL_BlitSurface(frappe[i],NULL,ecran,&persoPosition);

  SDL_Delay(sprite_vitesse);


}



}

}




if(event.key.keysym.sym==SDLK_SPACE)

{


if(face==1)
{persoPosition.y-=50;
persoPosition.x+=50;
for(i=0;i<5;i++)
{


  SDL_BlitSurface(saut[i],NULL,ecran,&persoPosition);

  SDL_Delay(sprite_vitesse);

}
}
if(face==2)
{
persoPosition.y-=50;
persoPosition.x-=50;
for(i=4;i<8;i++)
{

  SDL_BlitSurface(saut[i],NULL,ecran,&persoPosition);

  SDL_Delay(sprite_vitesse);


}
}
persoPosition.y+=50;
persoPosition.x+=50;


}








}




SDL_BlitSurface(jauge[0],NULL,ecran,&jaugeposition);

if(face==1)
{


SDL_Delay(50);


if(tir==1)
{

bulletposition1.x+=20;
SDL_BlitSurface(bullet1[i],NULL,ecran,&bulletposition1);
if(i>=3)
i=0;
i++;
 SDL_Delay(sprite_vitesse);

}
}

else if(face==2)
{
SDL_Delay(50);


if(tir==1)
{

bulletposition2.x-=20;
SDL_BlitSurface(bullet2[i],NULL,ecran,&bulletposition2);
if(i>=3)
i=0;
i++;
 SDL_Delay(sprite_vitesse);

}
}




SDL_BlitSurface(perso[cpt],NULL,ecran,&persoPosition);






  SDL_Flip(ecran);



}





SDL_FreeSurface(frappe[0]);
SDL_FreeSurface(trashs);
SDL_FreeSurface(perso[0]);
SDL_FreeSurface(bg);
SDL_FreeSurface(jauge[0]);

SDL_Quit();
return 0 ;



}

